<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: admincp_shareurl.php 32661 2013-02-28 06:29:46Z monkey $
 */

if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}


cpheader();
$operation = in_array($operation, array('add', 'edit', 'delete')) ? $operation : 'list';

loadcache('blockclass');

shownav('global', '分享网址');

if($operation=='add' || $operation=='edit') {
	if($operation=='edit') {
		showsubmenu('分享网址',  array(
			array('list', 'shareurl', 0),
			array('edit', 'shareurl&operation=edit'.'&id='.$_GET['id'], 1)
		));
	} else {
		showsubmenu('分享网址',  array(
			array('list', 'shareurl', 0),
			array('add', 'shareurl&operation=add', 1)
		));
	}

	include_once libfile('function/block');

		if(submitcheck('stylesubmit')) {
			$arr = array(
				'username' => trim($_POST['username']),
                'password' => trim($_POST['password']),
                'url' => trim($_POST['url']),
                'title' => trim($_POST['title']),
                'user' => trim($_POST['user']),
                'pass' => trim($_POST['pass']),
                'cid'=> trim($_POST['cid']),
			);

            if($arr['cid'] == "cate"){
            	cpmsg('操作失败,请选择分类名', 'action=shareurl&operation=add', 'succeed');
					exit;
            }

			if($_GET['id']) {
				$id = intval($_GET['id']);
			/*	$username = DB::fetch_first("SELECT id,username FROM pre_common_block_shareurl where username = '{$arr['username']}' and id != {$_GET['id']}");
				if($username){
					cpmsg('编辑失败,用户名已经存在', 'action=shareurl&operation=edit&id='.$id, 'succeed');
					exit;
				}*/
				C::t('common_block_shareurl')->update($id, $arr);
				require_once libfile('function/cache');
				updatecache('blockclass');
				cpmsg('编辑成功', 'action=shareurl', 'succeed');
			} else {
	           /* $username = DB::fetch_first("SELECT username FROM pre_common_block_shareurl where username = '{$arr['username']}'");
	            if($username){
	            	$msg = '用户名已经存在,不能进行添加，请重新输入';
					cpmsg($msg, 'action=shareurl&operation=add', 'succeed');
					exit;
	            }*/
				$arr['addtime'] = date('YmdHis');
				$id = C::t('common_block_shareurl')->insert($arr, true);
				require_once libfile('function/cache');
				updatecache('blockclass');
				$msg = '添加成功';
				cpmsg($msg, 'action=shareurl', 'succeed');
			}
		}

		if($_GET['id']) {
			$id = intval($_GET['id']);
			$thestyle = DB::fetch_first("SELECT * FROM pre_common_block_shareurl where id = '{$id}'");
			if(!$thestyle) {
				cpmsg('没有找到!');
			}
		} 


		showformheader('shareurl&operation='.$operation.'&id='.$_GET['id']);
		showtableheader();
		if($_GET['id']) {
			showtitle('编辑分享地址');
		} else {
			showtitle('添加分享地址');
		}
/*		$blockclass_sel = '<select name="blockclass">';
		$blockclass_sel .= '<option value="">'.cplang('blockstyle_blockclass_sel').'</option>';
		foreach($_G['cache']['blockclass'] as $key=>$value) {
			foreach($value['subs'] as $subkey=>$subvalue) {
				$blockclass_sel .= "<option value=\"$subkey\">$subvalue[name]</option>";
			}
		}
		$blockclass_sel .= '</select>';
		$adminscript = ADMINSCRIPT;
		$lang_blockclasssel = cplang('blockstyle_blockclass_sel');
		$lang_submit = cplang('submit');
		echo <<<BLOCKCLASSSEL
<form method="get" autocomplete="off" action="$adminscript">
	<div style="margin-top:8px;">
		<table cellspacing="3" cellpadding="3">
			<tr>
				<th>$lang_blockclasssel</th><td>$blockclass_sel</td>
			</tr>
			<tr>
				<th>&nbsp;</th>
				<td>
					<input type="hidden" name="action" value="blockstyle" />
					<input type="hidden" name="operation" value="add" />
					<input type="submit" value="$lang_submit" class="btn" />
				</td>
			</tr>
		</table>
	</div>
</form>
BLOCKCLASSSEL;*/
		showsetting('用户名', 'username', $thestyle['username'], 'text');
		showsetting('密码', 'password', $thestyle['password'], 'text');
		showsetting('地址', 'url', $thestyle['url'], 'text');
		showsetting('标题', 'title', $thestyle['title'], 'text');
		showsetting('表单name(用户名)', 'user', $thestyle['user'], 'text');
		showsetting('表单name(密码)', 'pass', $thestyle['pass'], 'text');
        $cate = C::t('common_block_cate')->fetch_all_by_where();
		showtablefooter();

        $cate = C::t('common_block_cate')->fetch_all_by_where();
		$template = '';
		$template .='<select name="cid">';
		if(!$_GET['id']){
		    $template .= "<option value='cate'>请选择分类</option>";
		}
        if($cate){
        	foreach ($cate as $key => $value) {
        		if($_GET['id']){
        			if($value[id] == $thestyle['cid']){
                        $template .= "<option value='$value[id]' selected>$value[name]</option>";
        			}else{
        			    $template .= "<option value='$value[id]'>$value[name]</option>";
        			}
        		}else{
        			$template .= "<option value='$value[id]'>$value[name]</option>";
        		}
        	}
        }
		$template .='</select><br/><br/>';
		$template .= '<input type="hidden" name="stylesubmit" value="1" />';
		$template .= '<br /><input type="submit" class="btn" value="'.$lang['submit'].'"></div><br /><br />';
		echo '<div class="colorbox">';
		echo '<div class="extcredits">';
		echo $template;
		echo '</div>';

		showformfooter();

 

} elseif($operation=='delete') {

	$_GET['id'] = intval($_GET['id']);
	$thestyle = C::t('common_block_shareurl')->fetch($_GET['id']);
	if(empty($thestyle)) {
		cpmsg('blockstyle_not_found', 'action=shareurl', 'error');
	}
	if(C::t('common_block_shareurl')->delete($_GET['id'])){
		updatecache('blockclass');
		cpmsg('删除成功', 'action=shareurl', 'succeed');	
	}else{
		updatecache('blockclass');
		cpmsg('删除失败', 'action=shareurl', 'succeed');	
	}
	echo "<pre>";
	var_dump(updatecache('blockclass'));exit;
	echo "</pre>";


} else {

	$_GET = $_GET + $_POST;
	$searchctrl = '<span style="float: right; padding-right: 40px;">'
				.'<a href="javascript:;" onclick="$(\'tb_search\').style.display=\'\';$(\'a_search_show\').style.display=\'none\';$(\'a_search_hide\').style.display=\'\';" id="a_search_show" style="display:none">'.cplang('show_search').'</a>'
				.'<a href="javascript:;" onclick="$(\'tb_search\').style.display=\'none\';$(\'a_search_show\').style.display=\'\';$(\'a_search_hide\').style.display=\'none\';" id="a_search_hide">'.cplang('hide_search').'</a>'
				.'</span>';
	showsubmenu('分享网址',  array(
		array('list', 'shareurl', 1),
		array('add', 'shareurl&operation=add', 0)
	), $searchctrl);

	$mpurl = ADMINSCRIPT.'?action=shareurl';
	$intkeys = array('id');
	$strkeys = array('blockclass');
	$randkeys = array();
	$likekeys = array('title');
	$results = getwheres($intkeys, $strkeys, $randkeys, $likekeys);
	foreach($likekeys as $k) {
		$_GET[$k] = dhtmlspecialchars($_GET[$k]);
	}
	$wherearr = $results['wherearr'];
	$mpurl .= '&'.implode('&', $results['urls']);

	$wheresql = empty($wherearr)?'1':implode(' AND ', $wherearr);

	$orders = getorders(array('blockclass'), 'id');
	$ordersql = $orders['sql'];
	if($orders['urls']) $mpurl .= '&'.implode('&', $orders['urls']);
	$orderby = array($_GET['orderby']=>' selected');
	$ordersc = array($_GET['ordersc']=>' selected');

	$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
	if(!in_array($perpage, array(10,20,50,100))) $perpage = 20;
	$perpages = array($perpage=>' selected');
	$mpurl .= '&perpage='.$perpage;

	$searchlang = array();
	$keys = array('search', 'likesupport', 'resultsort', 'defaultsort', 'orderdesc', 'orderasc', 'perpage_10', 'perpage_20', 'perpage_50', 'perpage_100',
	'blockstyle_id', 'blockstyle_name', 'blockstyle_blockclass', 'blockstyle_template');
	foreach ($keys as $key) {
		$searchlang[$key] = cplang($key);
	}
	$blockclass_sel = '<select name="blockclass">';
	$blockclass_sel .= '<option value="">'.cplang('blockstyle_blockclass_sel').'</option>';
	foreach($_G['cache']['blockclass'] as $key=>$value) {
		foreach($value['subs'] as $subkey=>$subvalue) {
			$selected = (!empty($_GET['blockclass']) && $subkey == $_GET['blockclass'] ? ' selected' : '');
			$blockclass_sel .= "<option value=\"$subkey\"$selected>$subvalue[name]</option>";
		}
	}
	$blockclass_sel .= '</select>';

	$adminscript = ADMINSCRIPT;
	echo <<<SEARCH
<form method="post" autocomplete="off" action="$adminscript" id="tb_search">
	<div style="margin-top:8px;">
		<table cellspacing="3" cellpadding="3">
			<tr>
				<th>$searchlang[blockstyle_id]</th><td><input type="text" class="txt" name="id" value="$_GET[id]"></td>
				<th>标题*</th><td><input type="text" class="txt" name="title" value="$_GET[title]">*$searchlang[likesupport]</td>
			</tr>
			<tr>
				<th>$searchlang[resultsort]</th>
				<td colspan="3">
					<select name="ordersc">
					<option value="desc"$ordersc[desc]>$searchlang[orderdesc]</option>
					<option value="asc"$ordersc[asc]>$searchlang[orderasc]</option>
					</select>
					<select name="perpage">
					<option value="10"$perpages[10]>$searchlang[perpage_10]</option>
					<option value="20"$perpages[20]>$searchlang[perpage_20]</option>
					<option value="50"$perpages[50]>$searchlang[perpage_50]</option>
					<option value="100"$perpages[100]>$searchlang[perpage_100]</option>
					</select>
					<input type="hidden" name="action" value="shareurl">
					<input type="submit" name="searchsubmit" value="$searchlang[search]" class="btn">
				</td>
			</tr>
		</table>
	</div>
</form>
SEARCH;
	$start = ($page-1)*$perpage;

	showformheader('shareurl');
	showtableheader('分享列表');
	showsubtitle(array('编号', '用户名', '密码','地址','表单name(用户名)','表单name(密码)','标题','添加时间', 'operation'));

	$multipage = '';
	if(($count = C::t(common_block_shareurl)->count_by_where($wheresql))) {
		include_once libfile('function/block');
		foreach(C::t('common_block_shareurl')->fetch_all_by_where($wheresql, $ordersql, $start, $perpage) as $value) {
			list($c1, $c2) = explode('_', $value['blockclass']);
			showtablerow('', array('class=""', 'class=""', 'class="td28"'), array(
				$value['id'],
				$value['username'],
				$value['password'],
				$value['url'],
				$value['user'],
				$value['pass'],
				$value['title'],
				$value['addtime'],
				"<a href=\"".ADMINSCRIPT."?action=shareurl&operation=edit&id=$value[id]\">".cplang('blockstyle_edit')."</a>&nbsp;&nbsp;".
				"<a href=\"".ADMINSCRIPT."?action=shareurl&operation=delete&id=$value[id]\">".cplang('blockstyle_delete')."</a>"
			));
		}
		$multipage = multi($count, $perpage, $page, $mpurl);
	}

	showsubmit('', '', '', '', $multipage);
	showtablefooter();
	showformfooter();

}

?>